﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LiteDB;
using RinNote.Server.Model.FileModel;
using RinNote.Server.Model;
using System.IO;
using RinNote.Server.Tool;

namespace RinNote.Server.Module
{
    public class NoteModule
    {
        /// <summary>
        /// 获得父文件夹下的子文件夹
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="parentFolderId">父文件夹ID 没有则为空</param>
        /// <returns></returns>
        public static Folder[] GetFoldersFromParentFolderId(Account user, string parentFolderId = "")
        {
            CheckFolder();
            using (var db = new LiteDatabase($"NoteDB/{user.AccountID}.db"))
            {
                var folders = db.GetCollection<Folder>("folders");
                var results = folders.Find(x => x.ParentFolderId == parentFolderId);
                return results.ToArray();
            }
        }
        /// <summary>
        /// 获得文件夹下的笔记
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="parentFolderId">父文件夹ID 为空则为顶部文件夹</param>
        /// <returns></returns>
        public static Note[] GetNotes(Account user, string parentFolderId = "")
        {
            CheckFolder();
            using (var db = new LiteDatabase($"NoteDB/{user.AccountID}.db"))
            {
                var notes = db.GetCollection<Note>("notes");
            }
            return null;
        }
        /// <summary>
        /// 创建一个文件夹
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="_folder">新建的文件夹对象</param>
        /// <returns>文件夹ID</returns>
        public static bool CreateFolders(Account user, Folder _folder)
        {
            CheckFolder();
            using (var db = new LiteDatabase($"NoteDB/{user.AccountID}.db"))
            {
                var folders = db.GetCollection<Folder>("folders");
                return folders.Upsert(_folder);
            }
        }
        /// <summary>
        /// 创建一个文件夹
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="_folderName">文件夹名称</param>
        /// <param name="_parentFolderId">父文件夹ID</param>
        /// <returns></returns>
        public static bool CreateFolders(Account user, string _folderName, string _parentFolderId = "")
        {
            Folder folder = new Folder() {
                _id=MD5Helper.GetMD5($"{_folderName},_,{_parentFolderId}"),
                CreateDate = DateTime.Now,
                CreateUserId = user.AccountID,
                FolderName = _folderName,
                HasPassword = false,
                ParentFolderId = _parentFolderId
            };
            return CreateFolders(user, folder);
        }
        /// <summary>
        /// 创建或修改一篇笔记
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="_note">新建笔记对象</param>
        /// <returns></returns>
        public static bool CreateNote(Account user, Note _note)
        {
            CheckFolder();
            using (var db = new LiteDatabase($"NoteDB/{user.AccountID}.db"))
            {
                var folders = db.GetCollection<Note>("notes");
                return folders.Upsert(_note);
            }
        }
        /// <summary>
        /// 创建或修改一篇笔记
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="_noteName">笔记名称</param>
        /// <param name="_parentFolderId">父文件夹ID</param>
        /// <returns></returns>
        public static bool CreateNote(Account user, string _noteName,string _title,string _content, string _parentFolderId = "")
        {
            Note note = new Note()
            {
                _id = MD5Helper.GetMD5($"{_noteName},_,{_parentFolderId}"),
                CreateDate = DateTime.Now,
                CreateUserId = user.AccountID,
                FolderId = _parentFolderId,
                HasPassword = false,
                NoteTitle=_title,
                Content=_content
            };
            return CreateNote(user, note);
        }
        /// <summary>
        /// 检查文件夹是否存在
        /// </summary>
        private static void CheckFolder()
        {
            if (!Directory.Exists("NoteDB"))
            {
                Directory.CreateDirectory("NoteDB");
            }
        }
    }
}
